<?php

namespace app\client\controller\collect;

use app\common\controller\Frontend;
use app\common\model\banxi\student\StudentLearnSubjectDuration;
use app\common\model\banxi\student\StudentLearnSubjectRecord;
use app\common\model\banxi\student\StudentSubjectTeachCollect;
use app\common\model\banxi\subject\SubjectTeach;
use app\common\model\banxi\subject\SubjectTeachChapter;
use app\common\model\banxi\trainer\Trainer as TrainerModel;
use think\db\exception\DbException;
use think\db\exception\PDOException;
use think\exception\ValidateException;
use think\facade\Validate;
use think\validate\ValidateRule;

/**
 * 课程收藏
 */
class courseCollect extends Frontend
{
    protected array $noNeedLogin = [];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
    }

    /*
     * 分页-课程收藏列表
     * */
    public function listCollectPage()
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query"]);

        $studentId = $this->auth->id;
        //查询条件解析
        $param['query']['EQ_studentId'] = $studentId;
        $where = get_query($param);

        //获取分页数据
        $pageParams = get_page_params($param);

        $StudentSubjectTeachCollectModel = new StudentSubjectTeachCollect();

        $list = $StudentSubjectTeachCollectModel
            ->where($where)
            ->order('id', 'desc')
            ->paginate([
                'page' => $pageParams['page'],
                'list_rows' => $pageParams['limit']
            ]);
        if ($list){
            $StudentLearnSubjectRecordModel = new StudentLearnSubjectRecord();
            $SubjectTeachModel = new SubjectTeach();
            $subjectTeachChapterMode = new SubjectTeachChapter();
            foreach ($list as $key=>$value)
            {
                $learnRecord = $StudentLearnSubjectRecordModel->where(['subjectTeachChapterId'=>$value['subjectTeachChapterId']])->find();
                $list[$key]['LearningProgress'] = $learnRecord ? $learnRecord['learningProgress'] : 0;
                $subjectTeach = $SubjectTeachModel->field('id,name,showImage')->find($value['subjectTeachId']);
                $subjectTeachChapter = $subjectTeachChapterMode->field('id,name')->find($value['subjectTeachChapterId']);
                $list[$key]['subjectTeach'] = $subjectTeach ? $subjectTeach : [];
                $list[$key]['subjectTeachChapter'] = $subjectTeachChapter ? $subjectTeachChapter : [];
            }
        }
        $this->ok('操作成功', result_row($list));
    }

    /**
     * 添加收藏
     * @return void
     * @throws DbException
     */
    public function addCollect(): void
    {
        $subjectTeachChapterId = $this->request->post('subjectTeachChapterId');

        $SubjectTeachChapterModel = new SubjectTeachChapter();
        $SubjectTeachChapter = $SubjectTeachChapterModel
            ->where(['id'=>$subjectTeachChapterId,'status'=>1])
            ->find();
        if (!$SubjectTeachChapter) {
            $this->error('课程章节不存在');
        }
        $SubjectTeachModel = new SubjectTeach();
        $subjectTeach = $SubjectTeachModel->where(['id'=>$SubjectTeachChapter['subjectTeachId'],'status'=>1])->find();
        if (!$subjectTeach) {
            $this->error('课程不存在');
        }

        $StudentSubjectTeachCollectModel = new StudentSubjectTeachCollect();
        $studentId = $this->auth->id;
        $data = [
            'studentId'=>$studentId,
            'subjectTeachId'=>$SubjectTeachChapter['subjectTeachId'],
            'subjectTeachChapterId'=>$SubjectTeachChapter['id']
        ];
        $collect = $StudentSubjectTeachCollectModel->where($data)->find();
        if (!$collect){
            $data['subjectId'] = $subjectTeach['subjectId'];
            $data['subjectGradeId'] = $subjectTeach['subjectGradeId'];
            $data['subjectTeachName'] = $subjectTeach['name'];
            $data['subjectTeachChapterName'] = $SubjectTeachChapter['name'];
            $StudentSubjectTeachCollectModel->save($data);
        }
        $this->ok('操作成功');
    }

    /**
     * 删除课程收藏
     * @return void
     * @throws DbException
     */
    public function delCollect(): void
    {
        $subjectTeachChapterId = $this->request->post('subjectTeachChapterId');


        $StudentSubjectTeachCollectModel = new StudentSubjectTeachCollect();
        $studentId = $this->auth->id;
        $where = [
            'subjectTeachChapterId'=>$subjectTeachChapterId,
            'studentId'=>$studentId
        ];

        $collect = $StudentSubjectTeachCollectModel->where($where)->find();
        if ($collect){
            $collect->delete();
        }
        $this->ok('操作成功');
    }
}